package com.second.service.impl;

import com.second.mapper.FGoodsMapper;
import com.second.model.pojo.FGoods;
import com.second.service.IFGoodsService;
import com.second.service.IFOrdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: MuTing
 * @date: 2022-05-21 21:17
 */
@Service
public class FGoodsServiceImpl implements IFGoodsService {

    @Autowired
    private FGoodsMapper fGoodsMapper;

    @Autowired
    private IFOrdService ordService;

    @Override
    public List<FGoods> findAllByShopId(Integer shopId) {
        List<FGoods> FGList = fGoodsMapper.findAllByShopId(shopId);
        for (FGoods fgl : FGList) {
            fgl.setGoodsSold(ordService.countGoodSold(fgl.getId()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //Date1 时间在 Date2 之后
            if (sdf.format(fgl.getGoodsEndDate()).compareTo(sdf.format(new Date()))<0){
                FGList.remove(fgl);
                break;
            }
        }
        return FGList;
    }

    @Override
    public FGoods getGoodsById(Integer goodId) {
        return fGoodsMapper.getById(goodId);
    }
}
